package com.findtech.threePomelos.base;

import android.Manifest;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.PixelFormat;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.IBinder;
import android.provider.Settings;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.view.WindowCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.graphics.Palette;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.findtech.threePomelos.R;
import com.findtech.threePomelos.activity.BabyInfoActivity;
import com.findtech.threePomelos.service.RFStarBLEService;
import com.findtech.threePomelos.utils.ToastUtil;
import com.findtech.threePomelos.view.dialog.CustomDialog;

/**
 * Created by zhi.zhang on 10/22/15.
 */
public class MyActionBarActivity extends AppCompatActivity implements Toolbar.OnMenuItemClickListener {
    protected MyApplication app = null;
    private Toolbar mToolbar;
    protected final int WRITE_EXTERNAL_STORAGE_REQUEST_CODE = 122;
    protected final int REQUEST_CODE_ASK_ACCESS_COARSE_LOCATION_PERMISSIONS = 123;
    protected final int REQUEST_CODE_ASK_ACCESS_FINE_LOCATION_PERMISSIONS = 124;
    protected final int REQUEST_CODE_ASK_SYSTEM_ALERT_WINDOW_PERMISSIONS = 125;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        app = (MyApplication) getApplication();
        supportRequestWindowFeature(WindowCompat.FEATURE_ACTION_MODE_OVERLAY);//长按出现复制粘贴栏在顶部占位问题解决方法
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (!Settings.canDrawOverlays(this)) {
                requestAlertWindowPermission();
            }
        }
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void onStart() {
        super.onStart();
    }

    @Override
    protected void onResume() {
        colorChange("ff6862");
        super.onResume();
    }

    protected void setToolbar(Toolbar toolbar, DrawerLayout drawerLayout) {
        toolbar.setTitle("");
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        /* findView */
        ActionBarDrawerToggle mDrawerToggle;
        mDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.drawer_open, R.string.drawer_close);
        mDrawerToggle.syncState();
        drawerLayout.setDrawerListener(mDrawerToggle);
    }

    protected void setToolbar(Toolbar toolbar, boolean canBack) {
        toolbar.setTitle("");
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(canBack);
    }

    protected void setToolbar(String title, boolean canBack) {
        mToolbar = (Toolbar) findViewById(R.id.custom_actionbar);
        Log.d("ZZ", "setToolbar");
        TextView mTextView = (TextView) findViewById(R.id.toolbar_title);
        if (title == null || title.isEmpty()) {
            mTextView.setText(getResources().getString(R.string.app_name));
        }
        mTextView.setText(title);
        mTextView.setTextColor(getResources().getColor(R.color.white));
        mTextView.setTextSize(20f);
        setToolbar(mToolbar, canBack);
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        // TODO Auto-generated method stub
        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
            View view = getCurrentFocus();
            if (isHideInput(view, ev)) {
                HideSoftInput(view.getWindowToken());
            }
        }
        return super.dispatchTouchEvent(ev);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Log.d("ZZ", "requestCode = " + requestCode + " resultCode = " + resultCode + " data = " + data);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (requestCode == REQUEST_CODE_ASK_SYSTEM_ALERT_WINDOW_PERMISSIONS) {
                if (!Settings.canDrawOverlays(this)) {
                    ToastUtil.showToast(MyActionBarActivity.this, "未打开权限,将无法确保婴儿推车数据正确", Toast.LENGTH_LONG);
                }
            }
        }
    }

    private void requestAlertWindowPermission() {
        final CustomDialog.Builder builder = new CustomDialog.Builder(this);
        builder.setTitle("提示");
        builder.setNotifyInfo("请务必打开权限，否则无法确保婴儿推车可以提供正确数据！");

        builder.setShowButton(true);
        builder.setPositiveButton("去设置", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION);
                intent.setData(Uri.parse("package:" + getPackageName()));
                startActivityForResult(intent, REQUEST_CODE_ASK_SYSTEM_ALERT_WINDOW_PERMISSIONS);
            }
        });

        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                ToastUtil.showToast(MyActionBarActivity.this, "取消设置权限,将无法确保婴儿推车数据正确", Toast.LENGTH_LONG);
            }
        });
        builder.create().show();
    }

    // 判定是否需要隐藏
    private boolean isHideInput(View v, MotionEvent ev) {
        if (v != null && (v instanceof EditText)) {
            int[] location = {0, 0};
            v.getLocationInWindow(location);
            int left = location[0], top = location[1], bottom = top + v.getHeight(), right = left + v.getWidth();
            if (ev.getX() > left
                    && ev.getX() < right
                    && ev.getY() > top
                    && ev.getY() < bottom) {
                return false;
            } else {
                return true;
            }
        }
        return false;
    }

    private void HideSoftInput(IBinder token) {
        if (token != null) {
            InputMethodManager manager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            manager.hideSoftInputFromWindow(token,
                    InputMethodManager.HIDE_NOT_ALWAYS);
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_MENU) {
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public boolean onMenuItemClick(MenuItem menuItem) {
        return false;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    // 判断是连接还是断开
    protected void connectedOrDis(String action) {
        if (RFStarBLEService.ACTION_GATT_CONNECTED.equals(action)) {
            Log.d(MyApplication.TAG, "111111111 连接完成");

        } else if (RFStarBLEService.ACTION_GATT_DISCONNECTED.equals(action)) {
            Log.d(MyApplication.TAG, "111111111 连接断开");
        }
    }

    /**
     * @param color String RGB 色值 eg. ff6862
     */

    protected void colorChange(String color) {
        if (mToolbar != null || TextUtils.isEmpty(color)) {
            if (android.os.Build.VERSION.SDK_INT >= 21) {
                Window window = getWindow();
                // 很明显，这两货是新API才有的。
                window.setStatusBarColor(colorBurn(Integer.parseInt(color, 16), 0.1f));
//                window.setNavigationBarColor(colorBurn(Integer.parseInt(color, 16), 0f));
            }
        }
    }

    protected void colorChange(String color, boolean hasToolbar) {
        if (hasToolbar != true || TextUtils.isEmpty(color)) {
            if (android.os.Build.VERSION.SDK_INT >= 21) {
                Window window = getWindow();
                window.setStatusBarColor(colorBurn(Integer.parseInt(color, 16), 0.1f));
            }
        }
    }

    /**
     * 颜色加深处理
     *
     * @param RGBValues RGB的值，由alpha（透明度）、red（红）、green（绿）、blue（蓝）构成，
     *                  Android中我们一般使用它的16进制，
     *                  例如："#FFAABBCC",最左边到最右每两个字母就是代表alpha（透明度）、
     *                  red（红）、green（绿）、blue（蓝）。每种颜色值占一个字节(8位)，值域0~255
     *                  所以下面使用移位的方法可以得到每种颜色的值，然后每种颜色值减小一下，在合成RGB颜色，颜色就会看起来深一些了
     * @return color
     */
    private int colorBurn(int RGBValues, float sub) {
        int alpha = RGBValues >> 24;
        int red = RGBValues >> 16 & 0xFF;
        int green = RGBValues >> 8 & 0xFF;
        int blue = RGBValues & 0xFF;

        red = (int) Math.floor(red * (1 - sub));
        green = (int) Math.floor(green * (1 - sub));
        blue = (int) Math.floor(blue * (1 - sub));
        return Color.rgb(red, green, blue);//E55D58
    }

}
